import os
import pandas as pd
from flask import Flask, render_template
from craw import craw

app = Flask(__name__)
app.secret_key = os.urandom(24)


# 爬虫首页
@app.route('/')
def index():
    return render_template('index.html')


# 开始爬取按钮
@app.route('/button1')
def button1():
    html = craw.get_html('北向')
    parse_list = craw.parse(html)
    craw.save_data(craw.craw_data(parse_list), '北向')
    return '爬取成功'


# 存入数据库按钮
@app.route('/button2')
def button2():
    craw.create_insert_table()
    return '保存成功'


# 开始爬取页面
@app.route('/column2/')
def column2():
    return render_template('column2.html')


# 读取列表页面
@app.route('/column3/')
def column3():
    filename = '北向数据.csv'
    data = pd.read_csv(filename, header=None, encoding='utf-8')
    return render_template('column3.html', data=data)


# 存入数据库页面
@app.route('/column4/')
def column4():
    return render_template('column4.html')


if __name__ == '__main__':
    app.run(host="0.0.0.0", debug=True)
